﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for checkall
/// </summary>
public class checkall
{
    public int a;
    SqlConnection conn;
    SqlCommand cmd;
    SqlDataReader dr;
	public checkall()
	{
       
	}



    public String checkint01(List<WebControl> a)
    {
        int number;
        float numbet01;
        foreach (WebControl ctl in a)
        {
            //kiem tra so integer
            if (ctl.Attributes["DataType"] == "integer")
                {
                    if ((!Int32.TryParse(((ITextControl)ctl).Text, out number)))
                    {
                        ctl.Focus();
                        ctl.BackColor = System.Drawing.Color.Yellow;
                        return "loi nhap so lieu";
                    }
                }

            //kiem tra so float
            if (ctl.Attributes["DataType"] == "float")
            {
                if ((!float.TryParse(((ITextControl)ctl).Text, out numbet01)))
                {
                    ctl.Focus();
                    ctl.BackColor = System.Drawing.Color.Yellow;
                    return "loi nhap so lieu";
                }
            }
            ctl.BackColor = System.Drawing.Color.White;
        }
        return "";
    }


    //kiem tra du lieu dau vao la list<textbox>
    // bien key kiem tra co cho phep postback o trang popup khong; key = 1 cho phep postback trang
    public String checkdata(List<WebControl> lst, String key)
    {
        datasever data = new datasever();
        conn = data.getconnect();
        conn.Open();
        
        foreach(WebControl wcl in lst)
        {
            MatchCollection mc;
            string chuoi = ((ITextControl)wcl).Text;
            chuoi = chuoi + " ";
            //tạo pattern
            //luật:cho tìm ra bất cứ những ký tự không phải ký tự khoảng trắng
            //rồi theo sau nó là kí tự khoảng trắng
            string pattern = @"\S+\s";
            Regex myRegex = new Regex(pattern);
            mc = myRegex.Matches(chuoi);
            if (mc.Count > 0)
            {
                for (int j = 0; j < mc.Count; j++)
                {
                    cmd = new SqlCommand("sp_check_if", conn);
                    cmd.Parameters.Add(new SqlParameter("@Ma", wcl.Attributes["keydata"]));
                    cmd.Parameters.Add(new SqlParameter("@Text", mc[j].Value));
                    cmd.CommandType = CommandType.StoredProcedure;
                    dr = cmd.ExecuteReader();
                    if (!dr.Read())
                    {
                        wcl.Focus();
                        wcl.BackColor = System.Drawing.Color.Yellow;
                        dr.Close();
                        conn.Close();
                        String s = mc[j].Value;
                        //cat ky tu cuoi cung cua chuoi "cat ky tu trang"
                        s = s.TrimEnd();
                        return "<script type=\"text/javascript\">window.open('popup.aspx?&Datatable=" + wcl.Attributes["keydata"] + "&Text=" + s + "&textboxid=" + wcl.ClientID + "&key=" + key + "','a','width=500,height=500,status=1');</script>";
                    }
                    dr.Close();
                }
            }
            else
            {
                cmd = new SqlCommand("sp_check_if", conn);
                cmd.Parameters.Add(new SqlParameter("@Ma", wcl.Attributes["keydata"]));
                cmd.Parameters.Add(new SqlParameter("@Text", ((ITextControl)wcl).Text));
                cmd.CommandType = CommandType.StoredProcedure;
                dr = cmd.ExecuteReader();
                if (!dr.Read())
                {
                    wcl.Focus();
                    wcl.BackColor = System.Drawing.Color.Yellow;
                    return "<script type=\"text/javascript\">window.open('popup.aspx?&Datatable=" + wcl.Attributes["keydata"] + "&Text=" + ((ITextControl)wcl).Text + "&textboxid=" + wcl.ClientID + "&key=" + key + "','a','width=500,height=500,status=1');</script>";
                }
                dr.Close();
            }
            
            wcl.BackColor = System.Drawing.Color.White;
        }
        conn.Close();
        return "";
    }

    public String checkDataString(String data, String Keydata)
    {
        //keydata tuong ung voi 1 bang trong csdl
        //kiem tra du lieu dung cho trang ajax
        datasever data1 = new datasever();
        conn = data1.getconnect();
        conn.Open();
        cmd = new SqlCommand("sp_check_if", conn);
        cmd.Parameters.Add(new SqlParameter("@Ma", Keydata));
        cmd.Parameters.Add(new SqlParameter("@Text", data));
        cmd.CommandType = CommandType.StoredProcedure;
        
        dr = cmd.ExecuteReader();
        if (!dr.Read())
        {
            dr.Close();
            conn.Close();
            return "";
        }
        dr.Close();
        conn.Close();
        return data;
    }
}